<?php
class Bbling_Model_Entity_Balanace extends Evebit_Model_Abstract {
	
	protected $_resourceName = "Bbling_Model_Resource_Balanace";
	
	protected $_resourceCollectionName = "Bbling_Model_Collection_Balanace";
	
	protected function _afterLoad() {
		return $this;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Evebit_Model_Abstract::_beforeSave()
	 */
	protected function _beforeSave() {
		return $this;
	}
	/**
	 * (non-PHPdoc)
	 * @return Evebit_Model_Abstract::_beforeDelete()
	 */
	protected function _beforeDelete() {
		return $this;
	}
	
	public function getBalanaceData($financialId) {
		$collection = $this->getCollection ();
		//$financialId = 15;
		$collection->addFieldToFilter ( 'main_table.financial_id', $financialId );
		$collection->setOrder ( 'main_table.month', 'ASC' );
		$items = $collection->getItems ();
		$datas = null;
		if (count ( $items )) {
			$monthName = array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jul', 'Jun', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
			foreach ( $items as $key => $item ) {
				$data = null;
				$data ['month'] = $monthName [$item->month - 1];
				$data ['sales'] = $item->sales;
				$data ['expenses'] = $item->salary + $item->payroll + $item->supplies + $item->advertising + $item->car_travel + $item->delivery + $item->utilities + $item->insurance + $item->accounts + $item->legal + $item->rent + $item->telephones + $item->interest + $item->taxes + $item->other - $item->cost_off;
				$datas [] = $data;
			}
			$financialEntity = new Bbling_Model_Entity_Financial ();
			$start_month = $financialEntity->load ( $financialId )->financial_start_month;
			$datas = $this->sortData ( $datas, $start_month );
		}
		
		return $datas;
	}
	
	public function sortData($datas, $start_month) {
		if (! ( int ) $start_month) {
			return;
		}
		$a = array_slice ( $datas, $start_month - 1, 12 - $start_month + 1, true );
		$b = array_slice ( $datas, 0, $start_month - 1, true );
		$c = array_merge ( $a, $b );
		
		return $c;
	}
	
	public function getBalanaceMonthData($option) {
		if (! isset ( $option ['month'] )) {
			throw new Zend_Exception ( 'Month can not empty' );
		}
		$collection = $this->getFinancialIdCollection ( $option );
		$collection->addFieldToFilter ( 'main_table.month', $option ['month'] );
		$data = $collection->getData ();
		
		return $data;
	}
	
	public function getFinancialIdCollection($option = null) {
		if (! isset ( $option ['financial_id'] )) {
			throw new Zend_Exception ( 'Finnancial id can not empty' );
		}
		$collection = $this->getCollection ();
		$collection->addFieldToFilter ( 'main_table.financial_id', $option ['financial_id'] );
		$collection->setOrder ( 'main_table.month', 'ASC' );
		
		return $collection;
	}
	
	public function getFinancialIdDatas($option = null) {
		$datas = $this->getFinancialIdCollection ( $option )->getData ();
		$financialEntity = new Bbling_Model_Entity_Financial ();
		$start_month = $financialEntity->load ( $option ['financial_id'] )->financial_start_month;
		if (count ( $datas )) {
			$datas = $this->sortData ( $datas, $start_month );
		}
		
		return $datas;
	}
	
	public function saveBalanaceDatas($saleDatas) {
		$month = intval ( $this->cost_off_month );
		$datas = $this->getResource ()->getTableData ( $this );
		$costOff = $datas ['cost_off'];
		unset ( $datas ['cost_off'] );
		foreach ( $saleDatas as $key => $sale ) {
			$cost_off = $month == $key ? $costOff : 0;
			$id = $this->getResource ()->getId ( $key, $datas ['financial_id'] );
			$this->addData ( $datas );
			$this->addData ( array ('id' => $id, 'sales' => $sale, 'month' => intval ( $key ), 'cost_off' => $cost_off ) );
			$this->save ();
			$this->unsetData ();
		}
	}
}